﻿@{
    ViewBag.Title = "用户列表";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<section class="content-header">
    <h1>
        管理员帐号管理
        <small></small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i>主页</a></li>
        <li class="active">管理员帐号管理</li>
    </ol>
</section>
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="box box-primary">
                <div class="box-header">
                    <form id="search-form" class="form-inline">
                        <div class="form-group">
                            <label>姓名</label>
                            <input type="text" class="form-control" style="width:200px;" maxlength="50" name="UserName" autocomplete="off" />
                        </div>
                        <div class="form-group">
                            <label>电话</label>
                            <input type="text" class="form-control" style="width:200px;" maxlength="11" name="UserMobile" autocomplete="off" />
                        </div>
                        <div class="form-group">
                            <button type="button" class="btn btn-success" id="btnSearch">查找</button>
                            <button class="btn btn-primary" id="btnAdd" type="button">添加</button>
                        </div>

                        <h3 class="box-title"></h3>
                    </form>
                </div>
                <!-- /.box-header -->
                <div class="box-body table-responsive no-padding">
                    <table id="table"></table>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="modal-editpwd" style="display: none;">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <h4 class="modal-title">修改密码</h4>
                </div>
                <div id="modal-body" class="modal-body">
                    <div class="form-group">
                        <label for="commentName">请输入新密码</label>
                        <input type="password" class="form-control" maxlength="50" id="newPwd" autocomplete="off" />
                    </div>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" onclick="savePwd()" data-dismiss="modal">确定</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
</section>
@section scriptsBottom{
    <script type="text/javascript">
        var addUrl = '@Url.Action("Add", "User")',
             deleteUrl = '@Url.Action("Delete", "User")',
             editPwdUrl = '@Url.Action("EditPwd", "User")',
             userListUrl = '@Url.Action("GetAdminUserList", "User")',
            statusUrl = '@Url.Action("SetStopStatus", "User")';

        //修改密码按钮
        function edit(userid) {
            var url = addUrl + "?userId=" + userid;
            location.href = url;
        }

        //删除
        function del(userid) {
            BMCW.DataConfirm("确定删除此用户?", function () {
                var url = deleteUrl + "?userId=" + userid;
                BMCW.ajax({
                    url: url,
                    type: "POST",
                    success: function (res) {
                        if (res.status == 0) {
                            BMCW.alert("删除成功");
                        } else {
                            BMCW.alert(res.message);
                        }
                        $('#table').bootstrapTable('refresh');
                    }
                });
            });
        }

        //停用启用
        function updateStopStatus(userId, status) {
            BMCW.ajax({
                url: statusUrl,
                type: "POST",
                data: {
                    "UserID": userId,
                    "IsStop": status
                },
                success: function (res) {
                    if (res.status == 0) {
                        if (status == 0) {
                            BMCW.alert("帐号启用成功");
                        } else {
                            BMCW.alert("帐号停用成功");
                        }
                        $('#table').bootstrapTable('refresh');
                    } else {
                        BMCW.alert(res.message);
                    }
                }
            });
        }

        $(document).ready(function () {
            $("#model_btn_yes").click(function () {
                updateVehiclStatus(2);
            });
            $("#model_btn_no").click(function () {
                updateVehiclStatus(3);
            });
            $("#btnAdd").click(function () {
               location.href = addUrl;
            });

            $("#btnSearch").on("click", function () {
                $('#table').bootstrapTable('refresh');
            })

            init();
        });

        //初始化
        function init() {
            $('#table').bootstrapTable({
                pagination: true,
                sortable: false,
                method: 'post',
                sidePagination: "server",
                queryParamsType:null,
                pageNumber: 1,
                pageSize: 10,
                pageList: [10, 25, 50],
                minimumCountColumns: 2,
                url: userListUrl,
                queryParams: function (params) {
                    var query = {
                        PageSize: params.pageSize,
                        PageIndex: params.pageNumber,
                        sortName: params.sortName,
                        sortOrder: params.sortOrder
                    };
                    var fdata = $("#search-form").serializeJson();
                    return $.extend(query, fdata);
                },
                columns: [{
                    field: 'UserID',
                    title: 'ID'
                }, {
                    field: 'UserName',
                    title: '用户名称'
                }, {
                    field: 'Mobile',
                    title: '电话'
                }, {
                    field: 'CompanyID',
                    title: '公司ID'
                }, {
                    field: 'CompanyName',
                    title: '公司名称'
                }, {
                    field: 'CreatedDate',
                    title: '创建时间'
                }, {
                    title: "操作",
                    align: 'center',
                    valign: 'middle',
                    width: 160, // 定义列的宽度，单位为像素px
                    formatter: function (value, row, index) {
                        var fmt='<button type="button" class="btn btn-info btn-xs" onclick="edit('+row.UserID+')">编辑</button>';
                        if (row.IsStop==0)
                        {
                            fmt+=' <button type="button" class="btn btn-info btn-xs" onclick="updateStopStatus('+row.UserID+',1)">停用</button>';
                        }else{
                            fmt+=' <button type="button" class="btn btn-info btn-xs" onclick="updateStopStatus('+row.UserID+',0)">启用</button>';
                        }
                        fmt += ' <button type="button" class="btn btn-info btn-xs" onclick="del(' + row.UserID + ')">删除</button>';
                        return fmt;
                    }
                }]
            });
        }
    </script>
}
